package com.kuaixueit.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuaixueit.crm.pojo.CFunction;
import com.kuaixueit.crm.pojo.ResultVO;
import com.kuaixueit.crm.service.FunctionService;

@Controller //实例化functionController放到spring容器
@RequestMapping("function") //窄化路径
public class FunctionController {
	
	@Autowired
	private FunctionService functionService;
	
	/**
	 * 根据父id查询子节点 
	 * @param pid
	 * @return
	 */
	@RequestMapping("nodes")
	@ResponseBody
	public List<HashMap> findNodesById(@RequestParam(required=true,defaultValue="0")Integer pid){
		List<HashMap> nodes = new ArrayList<HashMap>(); 
		try {
			nodes= functionService.findNodesByPid(pid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nodes;
	}
	/**
	 * 批量删除权限
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ResultVO deleteByIds(Integer id){
		try {
			functionService.deleteByPrimaryKey(id);
			return new ResultVO(0, null, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ResultVO("删除失败请重试");
	}
	
	
	/**
	 * 增加一个权限
	 * @param function
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public ResultVO add(CFunction function){
		function.setId(null);
		try {
			functionService.insert(function);
			return new ResultVO(0, null, "新增成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO("新增失败请重试");
		
	}
	
	/**
	 * 修改权限 必须带id
	 * @param function
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public ResultVO update(CFunction function){
		try {
			functionService.updateByPrimaryKey(function);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(0, null, "修改失败");
		}
		return new ResultVO(0, null, "修改成功");
	}

}
